﻿using AnlianCommon.Common.Domains.BankCustomers;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.Vendors
{
    public class ClientsUpdateHandler : IRequestHandler<ClientsUpdateCommand, Result>
    {
        private readonly IBaseRepository<Client> _clientRepository;

        public ClientsUpdateHandler(IBaseRepository<Client> clientRepository)
        {
            _clientRepository = clientRepository;
        }
        /// <summary>
        /// 客户信息修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(ClientsUpdateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result();
            try
            {
                var clientUpdate = await (await _clientRepository.GetAll()).Where(c => c.ClientId == request.ClientId).FirstOrDefaultAsync();
                if (clientUpdate != null)
                {
                    clientUpdate.UpdatedOwnerId = "张三";
                    clientUpdate.UpdatedTime = DateTime.Now;
                    var res = await _clientRepository.Update(clientUpdate);
                    if (res > 0)
                    {
                        result.Code=(int)CommonErrorCode.Success;
                        result.Msg = "修改成功";
                    }
                    else
                    {
                        result.Msg = "修改失败";
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
